package com.shujia.scala

object Demo2Base {
  /**
   * 文档注释
   *
   */
  def main(args: Array[String]): Unit = {
    //单行注释
    /*
    多行注释
     */

    //1、变量和常量
    //变量
    //类型自动推断，根据等号右边推断类型
    var i = 100
    var s: String = "大数据"
    //修改变量
    i = 200
    //多态：父类的引用指向子类的对象
    var j: Any = 100

    //常量
    val name: String = "张三"
    //name = "李四"

    //2、数据类型
    val age: Int = 20
    val PI: Double = 3.14159

    //3、字符串
    val line = "java,spark,scala"

    //返回一个数组
    //    val arr: Array[String] = line.split(",")
    val arr: Array[String] = line split ","
    //通过下标获取元素
    println(arr(1))

    //4、类型转换
    val k: String = "100"
    //使用java的方式
    val i1: Int = Integer.parseInt(k)
    println(i1)

    //scala用法
    //底层和java一样
    //scala通过隐式转换给对象动态增加新的方法
    val i2: Int = k.toInt
    println(i2)
    println(k.toDouble)

    //如果类型不匹配会出现类型转换异常
    //println("a".toInt)

    println(100.toLong)
    println((100 + 1 + 299 * 33).toString)

    //字符串拼接
    val id = "001"
    val name1 = "张三"
    val sex = "男"
    val age1 = 23

    //java字符串拼接
    val info = "id:" + id + ",name:" + name1 + ",sex:" + sex + ",age:" + age1
    println(info)
    //java字符串拼接
    val builder = new StringBuilder()
    builder.append("id:").append(id).append(",name:").append(name1)
    println(builder.toString())

    //scala字符串拼接
    //底层是StringBuilder
    val userInfo = s"id:$id,name:${name1.substring(0, 1)},sex:$sex,age:$age"
    println(userInfo)


    val n: String = null
    //空指针异常
    //println(n.split(","))

    //运算符
    var a = 100
    val b = 3
    println(a + b)
    println(a - b)
    println(a * b)
    println(a / b)
    println(a % b)
    //累加
    a += 1
    println(a)
    a -= 1
    println(a)
    a *= 2
    println(a)
    //标准写法
    println(a.+(b))
    //省略
    println(a + b)


  }
}
